package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type OperateRecordEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名
	DeviceName   string // 设备名称

	RecordName  string // 事件名称
	ClientModel string // 客户端模块
	OperateUuid string // 操作的UUID

	RecordParam map[string]interface{} // 配置参数
	RecordData  map[string]interface{} // 扩展参数
}

type operateRecordPo struct {
	Id           int64                  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`    // 设备类型名
	DeviceName   string                 `json:"deviceName" db:"device_name"`    // 设备名称
	RecordName   string                 `json:"recordName" db:"record_name"`    // 通道类型
	ClientModel  string                 `json:"clientModel" db:"client_model"`  // 通道名称
	OperateUuid  string                 `json:"operateUuid" db:"operate_uuid"`  // 通道名称
	RecordParam  map[string]interface{} `json:"recordParam" db:"record_param"`  // 配置参数
	RecordData   map[string]interface{} `json:"recordData" db:"record_data"`    // 扩展参数
}

func (e *OperateRecordEntity) Type() string {
	return "OperateRecordEntity"
}

func (e *OperateRecordEntity) TableName() string {
	return "tb_operate_record"
}

func (e *OperateRecordEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *OperateRecordEntity) Instance() *OperateRecordEntity {
	return &OperateRecordEntity{
		BaseEntity:  new(commEntity.BaseEntity),
		RecordParam: make(map[string]interface{}),
		RecordData:  make(map[string]interface{}),
	}
}

func (e *OperateRecordEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *OperateRecordEntity) NewPo() interface{} {
	return &operateRecordPo{
		RecordParam: make(map[string]interface{}),
		RecordData:  make(map[string]interface{}),
	}
}

func (e *OperateRecordEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *OperateRecordEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.DeviceName)
	return p
}

func (e *OperateRecordEntity) BuildByJson(jsn *string) error {
	var po operateRecordPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *OperateRecordEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *OperateRecordEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*operateRecordPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.RecordParam = po.RecordParam
	ptr.RecordData = po.RecordData
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.DeviceName = po.DeviceName
	ptr.ClientModel = po.ClientModel
	ptr.RecordName = po.RecordName
	ptr.OperateUuid = po.OperateUuid
}

func (e *OperateRecordEntity) BuildToPo() interface{} {
	ptr := e
	po := operateRecordPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.RecordParam = ptr.RecordParam
	po.RecordData = ptr.RecordData
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.DeviceName = ptr.DeviceName
	po.ClientModel = ptr.ClientModel
	po.RecordName = ptr.RecordName
	po.OperateUuid = ptr.OperateUuid

	return &po
}
